package cn.com.xy.sms.sdk.ui.popu.widget;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONObject;

import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.util.JsonUtil;
import cn.com.xy.sms.sdk.util.StringUtils;

public class DuoquProgressVerHolder {

    public TextView leftTitleView;
    public TextView rightHorizedContView;
    public TextView leftContentView;
    public ProgressBar progressBar;
    public Context mContext;
    public RelativeLayout mlineGroup;

    public LinearLayout mUsedLayout;

    private String mFLFlag = "\n";
    private static final String CONTENT_SINGLE = "1";

    public DuoquProgressVerHolder(Context context) {
        this.mContext = context;
    }

    public DuoquProgressVerHolder() {
    }

    public void setContent(int pos, BusinessSmsMessage message, String dataKey, boolean isReBind) {
        try {
            int top = (int) mContext.getResources().getDimension(R.dimen.duoqu_progress_item_first_top);
            if(pos == 0 && leftTitleView.getPaddingTop() != top){
                leftTitleView.setPadding(0, top, 0, 0);
            }

            JSONObject jsobj = (JSONObject) message.getTableData(pos, dataKey);
            String leftTitleText = (String) JsonUtil.getValFromJsonObject(jsobj, "t1");
            String leftContentText = (String) JsonUtil.getValFromJsonObject(jsobj, "t2");
            if (leftContentText.contains(mFLFlag)) {
                leftContentView.setMaxLines(99);
            } else {
                leftContentView.setMaxLines(2);
            }


            String used = (String) JsonUtil.getValFromJsonObject(jsobj, "r1");
            String remain = (String) JsonUtil.getValFromJsonObject(jsobj, "r2");
            leftTitleView.setText(leftTitleText);
            if (!StringUtils.isNull(used) && !StringUtils.isNull(remain)) {
                int progress_int = 0;
                try {
                    Float used_f = Float.parseFloat(used);
                    Float remain_f = Float.parseFloat(remain);
                    progress_int = (int) ((remain_f / (used_f + remain_f)) * 1000);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                progressBar.setMax(1000);
                progressBar.setProgress(progress_int);
                progressBar.setVisibility(View.VISIBLE);

                leftContentView.setText(leftContentText);
                leftContentView.setVisibility(View.VISIBLE);
                rightHorizedContView.setVisibility(View.GONE);
                leftTitleView.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            } else {
                progressBar.setVisibility(View.GONE);

                leftTitleView.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                leftContentView.setVisibility(View.GONE);
                rightHorizedContView.setVisibility(View.VISIBLE);
                rightHorizedContView.setText(leftContentText);
            }

            setViewStyle(jsobj, message, isReBind);
        } catch (Throwable ex) {
            LogManager.e("xiaoyuan", ex.getMessage(), ex);
        }
    }

    private void setViewStyle(JSONObject jsobj, BusinessSmsMessage message, boolean isReBind) {
        if (isReBind) {
            return;
        }
    }

    public void setVisibility(int visibility) {
        try {
            if (mUsedLayout != null) {
                mUsedLayout.setVisibility(visibility);
            } else {
                leftTitleView.setVisibility(visibility);
                leftContentView.setVisibility(visibility);
                progressBar.setVisibility(visibility);
            }
            if (mlineGroup != null) {
                mlineGroup.setVisibility(visibility);
            }
            if (leftContentView.getTag(R.id.tag_parent_layout) != null) {
                ((RelativeLayout) leftContentView.getTag(R.id.tag_parent_layout)).setVisibility(visibility);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
